<?php
include_once("core/mapped.php");

/**
 * User
 *
 * ModelClass mapped with user table to deal with a single user
 *
 * @copyright inovia 
 * @package oolime_core   
 */
class User extends Mapped {
  public $table = "user";
  
  /**
   * Adds an user with request's vars
   */     
  public function addUser() {
      $tab = Array(
        "user"  => "user",
        "password" => "password",
        "group" => "group"
        );
      $this->fromRequest($tab);
  }
  
  //not used i think
  public function getXML(){
    $tab = Array(
      "id"  => "uid",
      "user"  => "uuser",
      "password" => "upassword",
      "group" => "ugroup"
      );
		return $this->toXML("user",$tab);
  }
}

/**
 * Users
 *
 * Class dealing with a group of Users (instances of ModelClass_User) 
 * 
 * @copyright inovia
 * @version 1.4 2007/05/26
 * @package oolime_core   
 */  
class Users extends Normal{

  /**
   * Retrieves the list of Users
   * @return $UsersNode the node containing all users
   */     
  public function getUsers() {
  	$UsersNode = $this->response->doc->createElement("users");
  	
  	$req       = "SELECT * FROM `user`;";
  	$result    = $this->database->queries($req);
  	
  	while($obj = $result->fetch_object()) {
  		$UserNode = null;
      $tab = array(
        "id"=>"id",
        "user" => "user",
        "password"=>"password",
        "group"=>"group",
        "email"=>"email",
        "code"=>"code",
      );
      $UserNode = $this->response->mapObjectXML ($obj, "user", $tab);
      $UsersNode->appendChild($UserNode);
  	}
  	
    return $UsersNode;
  }
  
    /**
   * Retrieves a range of the list of Users
   * @param string filter (sql filter)
   * @return $UsersNode the node containing all users
   */     
  public function getUsersComplex($sql) {
  	$UsersNode = $this->response->doc->createElement("users");
  	
  	$req       = "SELECT * FROM `user` ".$sql.";";
  	$result    = $this->database->queries($req);
  	
  	if($result != FALSE){
    	while($obj = $result->fetch_object()) {
    		$UserNode = null;
        $tab = array(
          "id"=>"id",
          "user" => "user",
          "password"=>"password",
          "group"=>"group",
          "email"=>"email",
          "code"=>"code",
        );
        $UserNode = $this->response->mapObjectXML ($obj, "user", $tab);
        if($obj->lastLogin!=''){
          $LastLoginNode  = $this->response->doc->createElement("lastlogin");
          $LastLoginValue = $this->response->doc->createTextNode(date("d/m/y",$obj->lastLogin));
          $LastLoginNode->appendChild($LastLoginValue);
          $UserNode->appendChild($LastLoginNode);
        }
        $UsersNode->appendChild($UserNode);
    	}
    }
    return $UsersNode;
  }
  
  /**
   * Returns the number of users filtered
   * @param string sql filter
   * @return the number of users      
   */     
  public function getNumUsers($sql){
    $sqlCount = "SELECT COUNT(id) FROM `user` ".$sql;
    $result   = $this->database->queries($sqlCount);
    $array    = $result->fetch_array();
    
    return $array[0];
  }
}

?>
